#!/bin/bash

#Generates lots of files... 
#Default state, element, variable, title, description, distributions, install array
function add {
	echo "$3=\"$1\"" >> "def_$fil"
	echo "	$3=\"FALSE\"" >> "false_$fil"
	if [[ "$6" == "-" ]]; then
		for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
			echo "	\$$3 \"$2\" \"$3\" \"$4\" \"$5\" \\" >> "zen_$fil-${distribution_name_array[$i]}"
		done
	else
		echo "	\$$3 \"$2\" \"$3\" \"$4\" \"$5\" \\" >> "zen_$fil-$6"
	fi
	tmp="$3                    "
	tmp=$(echo "$tmp" | cut -c -20)
	tmp="$tmp      $4                         "
	tmp=$(echo "$tmp" | cut -c -50)
	echo "			echo \"  $tmp      $6\"" >> "para1_$fil"
	if [[ $3 == "-" ]]; then
		echo "Dummy"
	else
		count=$(expr $count + 1)
		if [[ $count == 1 ]]; then
			echo "				if [[ \${args[\$i]} == \"$3\" ]]; then" >> "para2_$fil"
		else
			echo "				elif [[ \${args[\$i]} == \"$3\" ]]; then" >> "para2_$fil"
		fi
		echo "					$3=\"TRUE\"" >> "para2_$fil"
		echo "					$install_array=\"\$$install_array $2\"" >> "para2_$fil"
	fi
}

#runs the add function for each distribution
function add_element {
	tmp=($6)
	for i in $(seq 0 $((${#tmp[*]} - 1))); do
		add "$1" "$2" "$3" "$4" "$5" "${tmp[$i]}"
	done
}

#Generates the "addonif"-file
#Default state, element, variable, title, description, distributions, install array
function add_add_on {
	add_element "$1" "$2" "$3" "$4" "$5" "$6"
	if [[ $3 == "-" ]]; then
		echo "Dummy"
	else
		count_add_on=$(expr $count_add_on + 1)
		if [[ $count_add_on == 1 ]]; then
			echo "	if [[ \"\${c[\$i]}\" == \"$3\" ]]; then" >> "addonif"
			echo "		$3" >> "addonif"
		else
			echo "	elif [[ \"\${c[\$i]}\" == \"$3\" ]]; then" >> "addonif"
			echo "		$3" >> "addonif"
		fi
	fi
}

function add_distribution {
	distribution_name="$distribution_name $1"
	distribution_text="$distribution_text $2"
	distribution_password_array[$distribution_count]="$3"
	distribution_check_array[$distribution_count]="$4"
	distribution_count=$(expr $distribution_count + 1)
}

function add_file {
	echo "################################################################################" >> "std-install.sh"
	echo "#FILE: $1" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	cat ./$1 >> "std-install.sh"
	echo "" >> "std-install.sh"
}

function samle {
	echo "#!/bin/bash" > "std-install.sh"
	echo "" >> "std-install.sh"
	echo "self=\$(readlink -f \$0)" >> "std-install.sh"
	echo "" >> "std-install.sh"
	echo "version=\"$version\"" >> "std-install.sh"
	echo "credits=\"Install script by Jonas Hansen. Current version \$version.\"" >> "std-install.sh"
	echo "" >> "std-install.sh"
	add_file check_version
	echo "#Remove" >> "std-install.sh"
	echo "#Default stuff" >> "std-install.sh"
	add_file def_remove
	echo "#Add-ons" >> "std-install.sh"
	echo "#Default stuff" >> "std-install.sh"
	add_file def_addon
	echo "#Install" >> "std-install.sh"
	echo "#Default stuff" >> "std-install.sh"
	add_file def_installer

	echo "" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	echo "#Determine distribution" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	echo "determine_distribution () {" >> "std-install.sh"
	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "	tmp=\$(${distribution_check_array[$i]} | grep \"${distribution_text_array[$i]}\")" >> "std-install.sh"
		echo "	if [[ \"\$tmp\" != \"\" ]]; then" >> "std-install.sh"
		echo "		distribution=\"${distribution_name_array[$i]}\"" >> "std-install.sh"
		echo "	fi" >> "std-install.sh"
	done
	echo "}" >> "std-install.sh"

	echo "################################################################################" >> "std-install.sh"
	echo "#Zenity checklist for remove" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "zenityremove_${distribution_name_array[$i]} () {" >> "std-install.sh"
		echo "zenity --list --title=\"Choose the programs to uninstall\" --width=1000 --height=500 --checklist --column=\"\" --column=\"Name\" --column=\"Variable\" --column=\"Title\" --column=\"Description\" --hide-column=2,3 --print-column=\$zenitycolumn \\" >> "std-install.sh"
		cat "./zen_remove-${distribution_name_array[$i]}" >> "std-install.sh"
		echo "" >> "std-install.sh"
		echo "}" >> "std-install.sh"
		echo "" >> "std-install.sh"
	done

	echo "################################################################################" >> "std-install.sh"
	echo "#Zenity checklist for add-ons" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "zenityaddons_${distribution_name_array[$i]} () {" >> "std-install.sh"
		echo "zenity --list --title=\"Choose the add-ons to install\" --width=1000 --height=500 --checklist --column="" --column=\"Name\" --column=\"Variable\" --column=\"Title\" --column=\"Description\" --hide-column=2,3 --print-column=\$zenitycolumn  \\" >> "std-install.sh"
		cat "./zen_addon-${distribution_name_array[$i]}" >> "std-install.sh"
		echo "" >> "std-install.sh"
		echo "}" >> "std-install.sh"
		echo "" >> "std-install.sh"
	done

	echo "################################################################################" >> "std-install.sh"
	echo "#Zenity checklist for install" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "zenityinstall_${distribution_name_array[$i]} () {" >> "std-install.sh"
		echo "zenity --list --title=\"Choose the programs to INSTALL\" --width=1000 --height=500 --checklist --column=\"\" --column=\"Name\" --column=\"Variable\" --column=\"Title\" --column=\"Description\" --hide-column=2,3 --print-column=\$zenitycolumn \\" >> "std-install.sh"
		cat "./zen_installer-${distribution_name_array[$i]}" >> "std-install.sh"
		echo "" >> "std-install.sh"
		echo "}" >> "std-install.sh"
		echo "" >> "std-install.sh"
	done

	echo "################################################################################" >> "std-install.sh"
	echo "#Set all checkboxes to false" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	echo "setallfalse () {" >> "std-install.sh"
	echo "	#Remove" >> "std-install.sh"
	add_file false_remove
	echo "	#Add-ons" >> "std-install.sh"
	add_file false_addon
	echo "	#Install" >> "std-install.sh"
	add_file false_installer
	echo "}" >> "std-install.sh"
	echo "" >> "std-install.sh"
	add_file parameters1
	add_file para1_addon
	add_file parameters2
	add_file para1_addon
	add_file para1_remove
	add_file parameters3
	add_file para1_installer
	add_file parameters4
	add_file para2_remove
	add_file para2_addon
	add_file para2_installer
	add_file parameters5
	add_file para2_remove
	add_file para2_addon
	add_file para2_installer
	add_file parameters6

	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "			echo \"  ${distribution_name_array[$i]}\"" >> "std-install.sh"
	done

	add_file parameters7

	echo "################################################################################" >> "std-install.sh"
	echo "#Password" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "if [[ \"\$distribution\" == \"${distribution_name_array[$i]}\" ]];then" >> "std-install.sh"
		echo "	${distribution_password_array[$i]}" >> "std-install.sh"
		echo "fi" >> "std-install.sh"
		echo "" >> "std-install.sh"
	done

	add_file "zenity"
	cat "std-install.sh" | sed 's/-="FALSE"//g' | tee "std-install2.sh"
	cat "std-install2.sh" | sed 's/$-/FALSE/g' | tee "std-install3.sh"
	rm "std-install.sh"
	mv "std-install3.sh" "std-install.sh"
	add_file addonfunktions
	echo "gnomepanel=0" >> "std-install.sh"
	echo "" >> "std-install.sh"
	add_file first-install
	echo "for i in \$(seq 0 \$((\${#c[*]} - 1))); do" >> "std-install.sh"
	add_file addonif
	echo "	fi" >> "std-install.sh"
	echo "done" >> "std-install.sh"

	echo "################################################################################" >> "std-install.sh"
	echo "#End" >> "std-install.sh"
	echo "################################################################################" >> "std-install.sh"
	for i in $(seq 0 $((${#distribution_name_array[*]} - 1))); do
		echo "if [[ \"\$distribution\" == \"${distribution_name_array[$i]}\" ]];then" >> "std-install.sh"
		cat "../../end-${distribution_name_array[$i]}" >> "std-install.sh"
		echo "fi" >> "std-install.sh"
		echo "" >> "std-install.sh"
	done
	add_file end
	chmod +x "std-install.sh"
}

count=0
count_add_on=0
distribution_count=0
